package com.unytech.project.entity.system;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Index;

import com.unytech.project.entity.BaseEntity;

/**
 * 权限组实体类
 * @author LONG
 *
 */
@Entity @Table(name="T_PERMISSIONGROUP")
@GenericGenerator(name="uuidpk",strategy="uuid")
public class PermissionGroup extends BaseEntity{
	@Id @GeneratedValue(generator="uuidpk")
	@Column(length=32)
    private String permgroupid;                //权限组ID
	
	@Index(name="permgroupname_index")        //这个是Hibernate的注解，换框架的时候，此处应注意
	@Column(length=30,unique=true,nullable=false)
    private String permgroupname;              //权限组名称 
	
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(nullable=false)
    private Date permgroupregdate = new Date();  //权限组注册日期
    
    @ManyToOne(cascade={CascadeType.REFRESH}, optional=false)  //每个权限组都必须有一个创建人， 并且不能为空 
    @JoinColumn(name="createperson_id")      //外键列名
    private User createperson;             //权限组创建人
    
    @Column(length=100,nullable=true)
    private String permgroupinfor;             //权限组简介
    
    @ManyToMany(cascade=CascadeType.REFRESH)
    @JoinTable(name="permissiongroup_permission",
	inverseJoinColumns=@JoinColumn(name="permission_id"),
		joinColumns=@JoinColumn(name="permissiongroup_id"))     //在关系维护段定义关联表以及关联表中的字段
    private Set<Permission> permissions = new HashSet<Permission>();    //权限组所和权限之间的多对多关系映射
    
	//级联刷新，级联更新；懒加载（一对多关系，多的一方默认就是延迟加载，所以可以不设置fetch；关系被维护端，由User类中的permgroup域来维护）
	@OneToMany(cascade={CascadeType.REFRESH},mappedBy="permgroup")
    private Set<User> users = new HashSet<User>();                   //隶属于此权限组下的所有用户，规定每个人只能隶属于一个权限组，所以是一对多关系
    
	public String getPermgroupid() {
		return permgroupid;
	}
	public void setPermgroupid(String permgroupid) {
		this.permgroupid = permgroupid;
	}
	public String getPermgroupname() {
		return permgroupname;
	}
	public void setPermgroupname(String permgroupname) {
		this.permgroupname = permgroupname;
	}
	public Set<Permission> getPermissions() {
		return permissions;
	}
	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setPermgroupinfor(String permgroupinfor) {
		this.permgroupinfor = permgroupinfor;
	}
	public String getPermgroupinfor() {
		return permgroupinfor;
	}
	public User getCreateperson() {
		return createperson;
	}
	public void setCreateperson(User createperson) {
		this.createperson = createperson;
	}
	public void setPermgroupregdate(Date permgroupregdate) {
		this.permgroupregdate = permgroupregdate;
	}
	public Date getPermgroupregdate() {
		return permgroupregdate;
	}

}
